﻿using Microsoft.AspNetCore.Mvc;
using SmartHotel.Dto;
using SmartHotel.HouseRoom.Dto;
using SmartHotel.Roles;
using SmartHotel.Rooms.Dto;
using SmartHotel.Users.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace SmartHotel.Rooms
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class SmartHotelAppRoomService : ApplicationService, ISmartHotelAppRoomService
    {
        private readonly IRepository<SmartHotel_RoomNumberTable, int> repository;
        private readonly IRepository<SmartHotel_HouseTable, int> repositoryhouse;

        public SmartHotelAppRoomService(IRepository<SmartHotel_RoomNumberTable,int> repository,  IRepository<SmartHotel_HouseTable, int> repositoryhouse) 
        {
            this.repository = repository;
            this.repositoryhouse = repositoryhouse;
        }
        /// <summary>
        /// 房号添加
        /// </summary>
        /// <param name="room"></param>
        /// <returns></returns>
        public async Task<ResultDto<RoomDto>> PostAddRoom(CreateandUpdateRoom room)
        {
            var data = ObjectMapper.Map<CreateandUpdateRoom, SmartHotel_RoomNumberTable>(room);
            await repository.InsertAsync(data);
            return new ResultDto<RoomDto> { Code=Result.Success,Message="添加成功",Data=data };
        }
        ///// <summary>
        ///// 房号显示
        ///// </summary>
        ///// <returns></returns>
        //public async Task<PageDto<HouseRoomDto>> GetRoom(int pageIndex,int pageSize,int? roomState=null,int? houseState=null)
        //{
        //    var roomlist = await repository.GetListAsync();
        //    var houseroomlist = await repositoryhouseroom.GetListAsync();
        //    var houselist = await repositoryhouse.GetListAsync();

        //    var data = from r in roomlist
        //               join hr in houseroomlist
        //               on r.Id equals hr.RoomId
        //               join h in houselist
        //               on hr.RoomNumberId equals h.Id
        //               select new HouseRoomDto()
        //               {
        //                   Id=r.Id,
        //                   HouseId = h.Id,
        //                   RoomNumberId = h.Id,
        //                   RoomId = r.Id,
        //                   RowHouse = hr.RowHouse,
        //                   CustomPrice = hr.CustomPrice,
        //                   RoomSort = r.RoomSort,
        //                   RoomRemark = r.RoomRemark,
        //                   RoomState = r.RoomState,
        //                   HouseState = r.HouseState,
        //                   RoomNumber = r.RoomNumber,
        //                   HouseName = h.HouseName,
        //                   HousePrice = h.HousePrice,
        //                   HouseSecurity = h.HouseSecurity,
        //                   HouseArea = h.HouseArea,
        //                   HousePeople = h.HousePeople,
        //                   HousePicture = h.HousePicture,
        //                   HouseType = h.HouseType,
        //                   ExtraBed = h.ExtraBed,
        //                   ExtraBedNumber = h.ExtraBedNumber,
        //               };
        //    data = data.WhereIf(roomState!=null,a=>a.RoomState== roomState).WhereIf(houseState !=null,a=>a.HouseState == houseState).ToList();
        //    var totalCount = data.Count();
        //    data = data.Skip((pageIndex-1)* pageSize).Take(pageSize).ToList();

        //    //var datas = ObjectMapper.Map<List<SmartHotel_RoomNumberTable>, List<RoomDto>>(data);
        //    return new PageDto<HouseRoomDto> {Code=Result.Success,Message="显示",Data= data, count= totalCount };
        //}
        /// <summary>
        /// 房号显示
        /// </summary>
        /// <returns></returns>
        public async Task<PageDto<RoomHouseDto>> GetRoom(int pageIndex, int pageSize, int? roomState = null, int? houseState = null)
        {
            var roomlist = await repository.GetListAsync();
            var houselist = await repositoryhouse.GetListAsync();

            var data = from r in roomlist
                       join h in houselist
                       on r.HouseId equals h.Id
                       select new RoomHouseDto()
                       {
                           Id = r.Id,
                           RoomSort = r.RoomSort,
                           RoomRemark = r.RoomRemark,
                           RoomState = r.RoomState,
                           HouseState = r.HouseState,
                           RoomNumber = r.RoomNumber,
                           HouseId = h.Id,
                           HouseName = h.HouseName,
                       };
            data = data.WhereIf(roomState != null, a => a.RoomState == roomState).WhereIf(houseState != null, a => a.HouseId == houseState).ToList();
            var totalCount = data.Count();
            // 执行分页操作
            var pagedData = data.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();


            //var datas = ObjectMapper.Map<List<SmartHotel_RoomNumberTable>, List<RoomDto>>(data);
            return new PageDto<RoomHouseDto> { Code = Result.Success, Message = "显示", Data = pagedData, count = totalCount };
        }

        ///// <summary>
        ///// 房号显示
        ///// </summary>
        ///// <returns></returns>
        //public async Task<PageDto<RoomGroupDto>> GetRoom(int pageIndex, int pageSize, int? roomState = null, int? houseState = null)
        //{
        //    var roomlist = await repository.GetListAsync();
        //    var houselist = await repositoryhouse.GetListAsync();

        //    var data = from r in roomlist
        //               join h in houselist on r.HouseId equals h.Id
        //               select new HouseRooml()
        //               {
        //                   Id = r.Id,
        //                   HouseId = h.Id,
        //                   RoomSort = r.RoomSort,
        //                   RoomNumber = r.RoomNumber,
        //                   RoomRemark = r.RoomRemark,
        //                   RoomState = r.RoomState,
        //                   HouseState = r.HouseState,
        //                   HouseName = h.HouseName,
        //                   HousePrice = h.HousePrice,
        //                   IsHouseSecurity = h.IsHouseSecurity,
        //                   HouseSecurity = h.HouseSecurity,
        //                   HouseArea = h.HouseArea,
        //                   HousePeople = h.HousePeople,
        //                   RoomShao = h.RoomShao,
        //                   HousePicture = h.HousePicture,
        //                   HouseType = h.HouseType,
        //                   ExtraBed = h.ExtraBed,
        //                   ExtraBedNumber = h.ExtraBedNumber,
        //                   GouseRank = h.GouseRank
        //               };

        //    var groupedData = data.GroupBy(x => x.HouseName)
        //                          .Select(g => new RoomGroupDto()
        //                          {
        //                              HouseName = g.Key,
        //                              HouseRoomList = g.ToList()
        //                          });

        //    var totalCount = groupedData.Count();
        //    var pagedData = groupedData.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

        //    return new PageDto<RoomGroupDto> { Code = Result.Success, Message = "显示", Data = pagedData, count = totalCount };
        //}


        /// <summary>
        /// 删除成功
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<RoomDto>> DeleteRoom(int id)
        {
            var data=await repository.FindAsync(id);
            await repository.DeleteAsync(data);
            return new ResultDto<RoomDto> {Code=Result.Success,Message="删除成功",Data=data };
        }
        /// <summary>
        /// 下架
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<RoomDto>> GetRoomState(int id)
        {
            var data= await repository.FindAsync(id);
            if (data.RoomState == 2)
            {
                data.RoomState = 1;
                await repository.UpdateAsync(data);
                return new ResultDto<RoomDto> { Code = Result.Success, Message = "修改成功",Data= data };
            }
            else if (data.RoomState == 1)
            {
                data.RoomState = 2;
                await repository.UpdateAsync(data);
                return new ResultDto<RoomDto> { Code = Result.Success, Message = "修改成功",Data= data };
            }
            return new ResultDto<RoomDto> { Code = Result.Success, Message = "修改失败" };
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<RoomDto>> GetFind(int id)
        {
            var data = await repository.FindAsync(id);
            return new ResultDto<RoomDto> { Code = Result.Success, Message = "反填成功", Data = data };
        }
        /// <summary>
        /// 房号修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultDto<RoomDto>> PutUpdRoom(CreateandUpdateRoom model)
        {
            var data = await repository.FindAsync(model.Id);
            var list = ObjectMapper.Map(model,data);
            await repository.UpdateAsync(list);
            return new ResultDto<RoomDto> { Code = Result.Success, Message = "修改成功", Data= list };
        }
    }
}
